package com.runa.rccprotocol.analysis;

import com.runa.monitor.comm.dto.newComm.PanelVO;
import com.runa.protocol.analysis.ShellBufferAnalysis;
import com.runa.protocol.enums.ByteOrder;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.hdrx.HDRXAck;
import com.runa.rccprotocol.dto.runa.RunaConcenResponseData;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.Security;

public class HDRXAnalysis extends ShellBufferAnalysis<byte[]> {

    private static final Logger logger = LoggerFactory.getLogger(HDRXAnalysis.class);

    static {
        Security.addProvider(new BouncyCastleProvider());
    }

    private final String key = "FUMXRNPXFUMXRNPX";

    public HDRXAnalysis() {
        super(null, 0);
    }

    @Override
    public int fill(byte[] data, int length, String netEquNo) {
        if (data[0] == (byte) 0xAA && data[1] == 0x72 && data[data.length-1] == (byte) 0xDD) {
            int len = ByteUtil.byteToShort(data, 2, ByteOrder.BA);
            int protoVer = data[4];
            int len2 = data[5];
            String sn = ByteUtil.byteToHexString(data, 6, len2);
            int len3 = ByteUtil.byteToShort(data, 6+len2, ByteOrder.BA);
            byte[] dec = decrypt(data, 6+len2, data.length - 8 - len2, key);
            if (dec != null) {
                System.arraycopy(dec, 0, data, 6+len2, dec.length);
            } else {
                return -1;
            }
            int start = 9+2*len2;
            if (data[9+2*len2] == 8) {
                PanelVO panelVO = new PanelVO();
                panelVO.setSerial(sn);
                panelVO.setHardVersion(String.valueOf(data[start+1] * 0.1));
                int alarm = data[start+2];
                panelVO.setBatteryVol((float) data[start+3]);
                panelVO.setSignal(String.valueOf(data[start+6]));
                String iccid = ByteUtil.byteToHexString(data, start+7, 11);
                panelVO.setTemperature((float) (ByteUtil.byteToShort(data, start+23, ByteOrder.BA) * 0.01));
                panelVO.setHumidity((float) (ByteUtil.byteToShort(data, start+25, ByteOrder.BA) * 0.01));
                RunaConcenResponseData responseData = new RunaConcenResponseData();
                responseData.addRepData(panelVO);
                responseData.setUpward(true);
                responseData.setAck(new HDRXAck(sn, key));
                this.interData = responseData;
            }
        }
        return 0;
    }

    public byte[] decrypt(byte[] hex, int off, int len, String key) {
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
            cipher.init(Cipher.DECRYPT_MODE,
                    new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES"));
            return cipher.doFinal(hex, off, len);
        } catch (Exception e) {
            logger.error("", e);
            return null;
        }
    }




    public static void main(String[] args) {
        byte[] bytes = ByteUtil.hexStringToBytes("AA725000100C10211001869691071584573FFA79A28B794C57CFBA10AA0A687A47BE466126D7F12711C6EB98D03034A4C76D6F25691860748AA98DEF715DAEFF77D1A929309C63CBD7448DC4C168964D2E8814DD");
        HDRXAnalysis analysis1 = new HDRXAnalysis();
        analysis1.fill(bytes, bytes.length, null);
        System.out.println();
    }
}
